﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors.DXErrorProvider;
using System.Text.RegularExpressions;
using GameDevelopHelper.Common;
using GameDevelopHelper;
using System.Configuration;
using System.Diagnostics;
using GameDevelopHelper.Configer;

namespace GameDevelopHelper
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        public static frmMain frmMain;
        private void frmLogin_Load(object sender, EventArgs e)
        {
            LoadProjectList();  //载入项目列表
            LoadConfig();       //载入配置
        }
        
        //加载项目列表（下拉框）
        private void LoadProjectList()
        {
            List<DataRow> ProjectRows = ProjectConfigerWrapper.GetProjectList();
            cmbProjectList.Items.Clear();
            foreach (DataRow row in ProjectRows)
            {
                cmbProjectList.Items.Add(row["ProjectName"].ToString());
            }
        }

        //加载配置
        private void LoadConfig()
        {
            //加载配置
            int ProjectId = LoginConfig.GetProjectId();
            if (cmbProjectList.Items.Count > ProjectId)
            {
                cmbProjectList.SelectedIndex = ProjectId;
            }

            txtUsername.Text = LoginConfig.GetLoginUsername();
            txtPassword.Text = LoginConfig.GetLoginPassword();

            chkAutoLogin.Checked = LoginConfig.GetAutoLogin();
            if (chkAutoLogin.Checked == false)
            {
                chkRememberPassword.Checked = LoginConfig.GetRememberPassword();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //项目为空
            if (cmbProjectList.Items.Count == 0)
            {
                StringTable.ResAlert("PROJECT_LIST_EMPTY", StringTable.ALERT_TYPE.WARNING);
                return;
            }

            //判断用户名合法性
            Regex UsernameRule = new Regex("^[A-Za-z][A-Za-z0-9_]{1,255}$");
            if (UsernameRule.IsMatch(txtUsername.Text) == false)
            {
                StringTable.ResAlert("USERNAME_WRONGFUL", StringTable.ALERT_TYPE.WARNING);
                txtUsername.Focus();
                txtUsername.SelectAll();
                return;
            }

            //密码为空
            if (txtPassword.TextLength == 0)
            {
                StringTable.ResAlert("PASSWORD_NOT_BLANK", StringTable.ALERT_TYPE.WARNING);
                txtPassword.Focus();
                txtPassword.SelectAll();
                return;
            }

            //登录
            bool bSuccess = Login(txtUsername.Text, txtPassword.Text);
            if (bSuccess == false)
            {
                StringTable.ResAlert("LOGIN_FAILED", StringTable.ALERT_TYPE.ERROR);
                txtPassword.Focus();
                txtPassword.SelectAll();
                return;
            }

            Program.g_bIsLoginFlag = true;

            //保存配置
            SaveLoginConfig();
            
            this.Close();
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        private void SaveLoginConfig()
        {
            LoginConfig.SaveProjectId(cmbProjectList.SelectedIndex);
            LoginConfig.SaveLoginUsername(txtUsername.Text);
            LoginConfig.SaveLoginPassword(txtPassword.Text);
            LoginConfig.SaveAutoLogin(chkAutoLogin.Checked);
            LoginConfig.SaveRememberPassword(chkRememberPassword.Checked);
        }

        /// <summary>
        /// 与远程服务器取得认证
        /// </summary>
        /// <param name="strUsername">用户名</param>
        /// <param name="strPassword">密码</param>
        /// <returns></returns>
        private bool Login(string strUsername, string strPassword)
        {
            return true;
        }

        private void btnProjectManager_Click(object sender, EventArgs e)
        {
            frmProjectManager frmMgr = new frmProjectManager();
            frmMgr.LoadOnlyManager();
        }

        private void chkAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAutoLogin.Checked == true)
            {
                chkRememberPassword.Checked = true;
            }
        }

        private void chkRememberPassword_CheckedChanged(object sender, EventArgs e)
        {
            if (chkRememberPassword.Checked == false)
            {
                chkAutoLogin.Checked = false;
            }
        }

        private void frmLogin_Shown(object sender, EventArgs e)
        {
            //体验细节
            if (txtUsername.TextLength == 0)
            {
                txtUsername.Focus();
                return;
            }

            if (txtPassword.TextLength == 0)
            {
                txtPassword.Focus();
                return;
            }

            btnLogin.Focus();
        }
    }
}
